//
//  MLGradientView.m
//  MoCloud
//
//  Created by xj on 2019/5/27.
//  Copyright © 2019 xj. All rights reserved.
//

#import "TJGradientView.h"

@implementation TJGradientView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.colors = @[UIColor_Hex(0x2e9def), UIColor_Hex(0x1e92fc)];
        [self setStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
    }
    return self;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    
    if (_unableGradient) {
        CAGradientLayer *layer = (CAGradientLayer *)self.layer;
        layer.colors = nil;
    }
}


+ (Class)layerClass {
    return CAGradientLayer.class;
}

- (void)setColors:(NSArray<UIColor *> *)colors {
    _colors = colors;
    
    NSMutableArray *MColors = @[].mutableCopy;
    CAGradientLayer *layer = (CAGradientLayer *)self.layer;
    [self.colors enumerateObjectsUsingBlock:^(UIColor *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [MColors addObject:(__bridge id)obj.CGColor];
    }];
    
    layer.colors = MColors.copy;
}


- (void)setStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
    CAGradientLayer *layer = (CAGradientLayer *)self.layer;
    layer.startPoint = startPoint;
    layer.endPoint = endPoint;
}

@end
